package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.conn.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    // /api/order/orderInfo/trade
    //确认订单
    @GuiGuLogin
    @PostMapping("/trade")
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo);
        return Result.ok(orderInfoVo);
    }

    // submitOrder
    // 提交订单
    @GuiGuLogin
    @PostMapping("/submitOrder")
    public Result<Map<String, String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        String orderNo = orderInfoService.submitOrder(orderInfoVo);
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return Result.ok(map);
    }


    //根据订单号获取订单信息
    @GuiGuLogin
    @GetMapping(value = "/getOrderInfo/{orderNo}")
    public Result<OrderInfo> findOrderInfoByOrderNo(@PathVariable(value = "orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        return Result.ok(orderInfo);
    }


    //获取用户订单
    @GuiGuLogin
    @GetMapping(value = "/findUserPage/{page}/{limit}")
    public Result<IPage<OrderInfo>> findOrderInfoByPage(@PathVariable(value = "page") Integer pageNo,
                                                        @PathVariable(value = "limit") Integer pageSize) {
        IPage<OrderInfo> infoIPage = orderInfoService.findOrderInfoByPage(pageNo, pageSize);
        return Result.ok(infoIPage);
    }
}

